package com.dahua.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class UserPreference {
    public static UserPreference userP = null;
    private static SharedPreferences sp = null;
    private static Editor ed = null;
    private static Context con = null;
    private static String name1 = "user";
    private static String lang = "lang";

    private UserPreference() {
    }

    public static UserPreference getInstance(Context ctx) {
        if (null == userP) {
            userP = new UserPreference();
            con = ctx;
            sp = con.getSharedPreferences(name1, Context.MODE_PRIVATE);
            ed = sp.edit();
        }
        return userP;
    }


    private boolean isLogin = false;
    private String uid;
    private String name;
    private String logonId;
    private int totalNum;
    private String role;
    private String countryId;
    private String data = "";
    private String status = "";
    private String pwd = "";

    public void setPwd(String pwd) {
//        this.pwd = pwd;
        ed.putString("pwd", pwd);
        ed.commit();
    }

    public String getPwd() {
//        return pwd;
        return sp.getString("pwd", pwd);
    }

    public void setLang(String lang) {
        ed.putString("lang", lang);
        ed.commit();
    }

    public String getLang() {
        return sp.getString("lang", "en");
    }


    public String getData() {
        return sp.getString("data", data);
    }

    public void setData(String data) {
        ed.putString("data", data);
        ed.commit();
    }
    public int getTotal() {
        return sp.getInt("totalNum", totalNum);
    }

    public void setTotal(int totalNum) {
        ed.putInt("totalNum", totalNum);
        ed.commit();
    }

    public String getLogonId() {
        return sp.getString("logonId", logonId);
    }

    public void setLogonId(String logonId) {
        ed.putString("logonId", logonId);
        ed.commit();
    }

    public String getRole() {
        return sp.getString("role", role);
    }

    public void setRole(String role) {
        ed.putString("role", role);
        ed.commit();
    }

    public String getCountryId() {
        return sp.getString("countryId", "");
    }

    public void setCountryId(String countryId) {
        ed.putString("countryId", countryId);
        ed.commit();
    }

    public void setGetUserInfo(boolean isGetUserInfo) {
        ed.putBoolean("isGetUserInfo", isGetUserInfo);
        ed.commit();
    }

    public String getUid() {
        return sp.getString("uid", uid);
    }

    public void setUid(String uid) {
        ed.putString("uid", uid);
        ed.commit();
    }

    public String getName() {
        return sp.getString("name", name);
    }

    public void setName(String name) {
        ed.putString("name", name);
        ed.commit();
    }

    public String getStatus() {
        return sp.getString("status", status);
    }

    public void setStatus(String status) {
        ed.putString("status", status);
        ed.commit();
    }


    public void setLanguageEnglish(int languageEnglish) {
        ed.putInt("languageEnglish", languageEnglish);
        ed.commit();
    }


    public void setrCloudToken(String rCloudToken) {
        ed.putString("rCloudToken", rCloudToken);
        ed.commit();
    }


    public void setLat(String lat) {
        ed.putString("lat", lat);
        ed.commit();
    }


    public void setLng(String lng) {
        ed.putString("lng", lng);
        ed.commit();
    }


    public void setCreateNewMsgNum(String createNewMsgNum) {
        ed.putString("createNewMsgNum", createNewMsgNum);
        ed.commit();
    }

    public void setHistoryNewMsgNum(String historyNewMsgNum) {
        ed.putString("historyNewMsgNum", historyNewMsgNum);
        ed.commit();
    }


    public void setTradeIndex(int tradeIndex) {
        ed.putInt("tradeIndex", tradeIndex);
        ed.commit();
    }


    public String getActivityCommentNum(String activityId) {
        return sp.getString("activityId", "");
    }

    public void setActivityCommentNum(String activityId, String num) {
        ed.putString("activityId", num);
        ed.commit();
    }


    public void setSessionKey(String sessionKey) {
        ed.putString("sessionKey", sessionKey);
        ed.commit();
    }


    public void setEncrypt(boolean encrypt) {
        ed.putBoolean("encrypt", encrypt);
        ed.commit();
    }


    public void setActualOrVirtual(String actualOrVirtual) {
        ed.putString("actualOrVirtual", actualOrVirtual);
        ed.commit();
    }


    public void setAccount(String account) {
        ed.putString("account", account);
        ed.commit();
    }

    public void setSid(String sid) {
        ed.putString("sid", sid);
        ed.commit();
    }


    public void setDid(String did) {
        ed.putString("did", did);
        ed.commit();
    }

    public boolean isLogin() {
        return sp.getBoolean("isLogin", isLogin);
    }

    public void setLogin(boolean login) {
        ed.putBoolean("isLogin", login);
        ed.commit();
    }


    private void putData(String key, Object value) {
        if (value instanceof Boolean) {
            ed.putBoolean(key, (Boolean) value);
        } else if (value instanceof String) {
            ed.putString(key, value.toString());
        } else if (value instanceof Integer) {
            ed.putInt(key, (Integer) value);
        } else if (value instanceof Float) {
            ed.putFloat(key, (Float) value);
        }
        // TODO: other data types
        ed.commit();
    }
}
